package com.gopay.common.base.filter;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.common.util.RequestUtils;

/**
 * RequestFilter
 * 
 * @RequestFilter.java
 * @author yaobo
 * @2013-5-7 上午09:23:38 www.gopay.com.cn Inc.All rights reserved.
 */
public class RequestFilter implements Filter {
    private static Log logger = LogFactory.getLog(RequestFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequest request = (HttpServletRequest) req;

        StringBuffer sb = new StringBuffer();
        sb.append(RequestUtils.getRemoteRealIpAddr(request) + " ");
        sb.append(new Date(System.currentTimeMillis()).toString() + " ");
        sb.append(request.getMethod() + " ");
        sb.append(request.getRequestURI());

//        logger.error(sb.toString());
        
        if (request.getParameterNames() != null) {
            for (Enumeration<?> enumeration = request.getParameterNames(); enumeration.hasMoreElements();) {
                String eleName = (String) enumeration.nextElement();
                String eleValue = request.getParameter(eleName);
                if ("verficationCode".equalsIgnoreCase(eleName)) {
                    logger.error("收到的请求参数键: [" + eleName + "]，值: [" + StringUtils.abbreviateMiddle(eleValue, "*", 6) + "]");
                 // edit hougn 2017-02-16 pci认证,密码掩码打印  start
                } else if("mtPaypassword".equalsIgnoreCase(eleName) || "passwordConfirm".equalsIgnoreCase(eleName)
                		|| StringUtils.containsIgnoreCase(eleName, "pwd")) {
                    logger.error("收到的请求参数键: [" + eleName + "]，值: [******]");
                } else if("cvn2".equalsIgnoreCase(eleName) || "availableDate".equalsIgnoreCase(eleName)
                        || "mtcode".equalsIgnoreCase(eleName)) {
                    logger.error("收到的请求参数键: [" + eleName + "]，值: [" + eleValue.replaceAll("\\d", "*") + "]");
                 // edit hougn 2017-02-16 pci认证,密码掩码打印  start
                } else {
//                    logger.error("收到的请求参数键: [" + eleName + "]，值: [" + eleValue + "]");
                	//LoggerMaskUtils.checkAndMaskString  此方法主要是针对pci做的改造，请勿修改, ssj 2017/02/10
                    logger.error("收到的请求参数键: [" + eleName + "]，值: [" + LoggerMaskUtils.checkAndMaskString(eleName, eleValue) + "]");
                }
            }
        }

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
